import Vue from '../..//node_modules/vue/dist/vue.js'
import Vuex from 'vuex'

import axios from 'axios'

Vue.use(Vuex)

import { Toast, MessageBox, Indicator} from 'mint-ui'

const store = new Vuex.Store({

    state:sessionStorage.getItem('state') ? JSON.parse(sessionStorage.getItem('state')):{

        isRouterAlive: true,
        isLogin: false,

        //购物车的数据
        //搜索关键字 首页 每次重新获取数据的时候要置空
        keyWord: '',
        //购物车结算钱数
        carTotal: 0,
        //点击进入查看商店的时候 保存该index 不然刷新数据就没了
        readIndex: '',
        //购物车清单
        carMenu:[],
        //优惠卷
        couponMenu:[],
        //我的优惠卷
        myCouponMenu: [],
        //购物车当前选择食材的商店的 id
        shopOrderId: '',//用于筛选 优惠劵 以及标识点击复选框 是否跨商店了 如果是，就把总价钱置为 0

        //订单数据
        alerOrder:[],//我的个人中心已经完成的订单

        isActive: 'home',//tabBar图标的显示
        showTab: true,//tabBar是否显示

        type: '普通用户',//默认普通用户
        showShop: false,//默认是普通用户，没有设置商店名字选项
        uname: '',//用户名
        email: '',//邮箱
        hImg: '',//头像
        storeFlag: '',//每个用户的store标志注册时返回的id

        //是否显示图片预览
        isReadImg: false,
        //当前预览的图片  因为是预览当前的图片会用到 v-show但是 foodName一开始
        //打开商铺页面是undefind 会报错 因此这里初始化一个 foodName给他  至于其他为什么不报错
        //还不知道为什么
        currentFood: {data:[{foodName:''}]},
        foodTypeIndex: 0,
        foodCurrentIndex: 0,
        homeShopList: [],

        //菜单栏管理
        ItemDelect: false,//左菜单编辑
        ItemEdit: false,//左菜单删除
        isManger: false,//是否点击管理
        //店家信息
        shopPhone: '',//电话
        shopAddress: '',//地址
        shopBgc: '',//背景
        sname: '',//商铺名字
        shopId: '',//商店id 其实就是storeFlag
        //这个是谁开的店铺  即使 一开始创建店铺的时候等于用户名、
        //之后即使用户名修改了 这个值也不需要在修改了  标志该店铺即可
        statusName: '',
        goodMenu: [//清单           
        ]
    },
    mutations:{
        

        //设置 state属性
        setState(state, setState){
            for(let item in setState){
                state[item] = setState[item]
            }
        },
        
        //提示信息
        setToast(state, msg){
            Toast(msg)
        },
        
        setIndicator(state, text){
            Indicator.open({
                text,
                spinnerType: 'fading-circle'
            })
        },

        setMessage(state, msg){
            MessageBox.alert(msg)
        },

        //退出登陆
        logout(state, {router}){
            MessageBox({
                title: '提示',
                message: '是否退出登陆',
                showCancelButton: true
            }).then((action) =>{
                if(action === 'cancel') return
                sessionStorage.removeItem('selected')
                sessionStorage.removeItem('state')
 
                this.dispatch('saveState', {uname: state.uname, storeFlag: state.storeFlag, router})         
            })
        }
    },
    getters:{
       
    },
    actions:{


//------------------------------------登陆注册
        //注册
        registerCount({state}, data){

            return new Promise((resolve, reject) =>{
                            //data 即为当前发送到后台的数据
                axios({
                    method: 'post',
                    responseType: 'json',
                    url: 'http://localhost:3003/register',
                    data
                }).then((res) =>{
                    const {code, storeFlag} = res.data
                    console.log(code)
                    if(code === 1) return resolve(storeFlag)

                    reject()

                })
            })
        },
        //登陆
        login({state}, params){
            return new Promise((resove, reject) =>{
                axios({
                    method: 'get',
                    responseType: 'json',
                    url: 'http://localhost:3003/login',
                    params
                }).then((res) =>{
                    const {storeFlag, msg, code} = res.data

                    //用户名或者密码错误 传入提示信息
                    if(code === -1 || code === -2) return reject(msg)
                    //否则可以登陆
                    resove(storeFlag)
                })
            })
        },

        //修改用户的注册信息
        changeLogin({dispatch, commit, state, getters},data){
            console.log(data)
            return new Promise((resove, reject) =>{
                axios({
                    method: 'post',
                    responseType: 'json',
                    url: 'http://localhost:3003/changeLogin',
                    data
                })
                    .then((res) =>{
                    const {code, data} = res.data
                    if(code === 1) return resove(code)

                    reject(code)
                })
            })
        },

//----------------------------------------vuex状态的
        //拿取state的数据  登陆拿取的数据
        getState({commit, state, getters},{storeFlag}){
            return new Promise((resove, reject) =>{
                axios({
                    method: 'get',
                    responseType: 'json',
                    url: 'http://localhost:3003/getState',
                    params:{
                        storeFlag
                    }
                })
                  .then((res) =>{
                    
                    const {code, data} = res.data
                    if(code === -1) return MessageBox.alert('登陆失败, 请稍后再试')
                    //此时这里应该初始化它的值  是向服务器拿取数据
                    store.replaceState(data)
                    //调用成功回调
                    resove()
                })
            })
        },

        //第一次注册保存state的数据  此时保存state数据都是使用 storeFlag来修改
        saveState({commit, state, getters},{uname, storeFlag, router}){
            axios({
                method: 'post',
                url:'http://localhost:3003/saveState',
                data:{
                    storeFlag,
                    uname,
                    state
                }
            }).then((res) =>{
                if(res.data.code === 1){
                    console.log('保存成功') 
                    if(router) {

                        //直接取初始化的state
                        store.replaceState(JSON.parse(sessionStorage.getItem('stateInit')))
                        //说明是退出登录
                        router.replace('/login')
                    }      
                }
            })
        },

//------------------------------------------商家
        saveShop({commit, state, getters}, data){
            return new Promise((resolve, reject) =>{
                axios({
                    method: 'post',
                    url: 'http://localhost:3003/saveShop',
                    data
                }).then(res =>{
                  const {data, code} = res.data
                  if(code === 1) return resolve()

                  reject()
                })
            })
        },

//--------------------------------------------------------------修改用户名 以及商店名字 其他数据也相应改变
        //修改用户名 以及商店名字 其他数据也相应改变
        changeNS({commit, state, getters},{data, url}){
            return new Promise((resolve, reject) =>{
                //name 即为修改之后用户名 或者商店名字
                //changeUName 是否是修改用户名
                //是否是商家 不是商家就不需要修改 商店的相应内容
                axios({
                    method: 'post',
                    url:'http://localhost:3003/'+ url,
                    data:{
                        data,
                        id: state.storeFlag
                    }
                }).then((res) =>{
                    const {msg, code} = res.data
                    console.log(code)
                    if(code === 1) return resolve(msg)
                    reject(msg)
                })
            })
        },
//----------------------------------------购物车的
        getCar({dispatch, commit, state, getters}, orderId){

            const {data, router, to} = dataObj

            axios({
                method: 'get',
                responseType: 'json',
                url: 'http://localhost:3003/getCar',
                data:{
                    orderId
                }
            })
            .then((res) =>{
                
                if(res.data.code === 1){
                    console.log(res.data)
                }
            })
        },

//----------------------------------------登陆注册的

//----------------------------------------优惠卷的发放 领取
        //发放
        sendCoupon({commit, state, dispatch, getters}, {data, couponId, take}){
           return new Promise((resolve, reject) =>{
                axios({
                    method: 'post',
                    responseType: 'json',
                    url: 'http://localhost:3003/sendCoupon',
                    data
                }).then((res) =>{

                    const {code} = res.data

                    if(code === 1) resolve()

                    reject()
                })
           })
         },
       
         //店家删除 一旦店家删除所有用户都不能使用
         removeCoupon({commit, state, getters}, {couponFlag, data}){

            return new Promise((resolve, reject) =>{
                axios({
                    method: 'post',
                    responseType: 'json',
                    url: 'http://localhost:3003/removeCoupon',
                    data:{
                        couponFlag,
                        data
                    }
                }).then((res) =>{

                    const {msg, code} = res.data

                    if(code === 1) return resolve(msg)
                    
                    reject(msg)
                })
            })
         },

         //顾客删除和使用优惠劵
         useCoupon({dispatch, commit, state, getters}, {couponFlag, couponId, data}){
            return new Promise((resolve, reject) =>{
                axios({
                    method: 'post',
                    url: 'http://localhost:3003/delectCoupon',
                    data: {
                        couponFlag,
                        couponId,
                        data
                    }
                }).then(res =>{


                    const {data, code} = res.data


                    if(code === 1) return resolve()

                    reject()
                })
            })
         },

         //验证商店有没有破产
         checkShop({dispatch, commit, state, getters}, {shopId}){
             return new Promise((resolve, reject) =>{
                axios({
                    method: 'get',
                    url: 'http://localhost:3003/checkShop',
                    params: {
                        shopId
                    }
                }).then((res) =>{
                    const {code, msg} = res.data
                    if(code === 1) return resolve()

                    reject()
                })
            })
        },

         //用来修改当用户名或者 商店名字修改之后 修改优惠劵相应的uname 以及 sname
         changeCouponInfo({dispatch, commit, state, getters}, {belong, data}){
            axios({
                method: 'post',
                url: 'http://localhost:3003/delectCoupon',
                data: {
                    belong,
                    data
                }
            }).then(res =>{
                if(res.data.code === 1){

                }
            })
         },
        
         //拿取优惠劵
         getCoupon({dispatch, commit, state, getters}, {couponId}){
             return new Promise((resolve, reject) =>{
                axios({
                    method: 'get',
                    responseType: 'json',
                    url: 'http://localhost:3003/getCoupon',
                    params:{
                        couponId
                    }
                }).then(res =>{
                    const {data, code} = res.data
                    if(code === 1) return resolve(data)

                    reject()
                })
             })
         },

         //领取优惠卷
         takeCoupon({commit, state, getters}, {couponId, couponSign}){
           return new Promise((resolve, reject) =>{
                axios({
                    method: 'get',
                    responseType: 'json',
                    url: 'http://localhost:3003/takeCoupon',
                    params:{
                        couponId,
                        couponSign
                    }
                }).then((res) =>{
                    const {code, data} = res.data
                    console.log(res)
                    if(code === 1) resolve(data)

                    reject()
                })
           })
         },

//----------------------------------------订单
        saveOrder({commit, state, getters},{orderUName, orderSName, otherOrderId, shopOrder, oFlag, orderId, orderGet}){

            axios({
                method: 'post',
                url:'http://localhost:3003/saveOrder',
                data:{
                    shopOrder,
                    oFlag,
                    orderUName,
                    orderSName,
                    otherOrderId,
                    orderFlow: 0,
                    orderRefundCommit: '-',
                    isCommit: false,
                    orderState: 0,
                    orderId,
                    orderGet
                }
            }).then((res) =>{
                if(res.data.code === 1){
                    //关闭即可
                    Indicator.close()
                }
            })
        },

        getMyOrder({commit, state, getters}){
            axios({
                method: 'get',
                url:'http://localhost:3003/getMyOrder',
                params:{
                    orderId: state.storeFlag,
                    orderState: 1
                }
            }).then((res) =>{
                const {data, code} = res.data
               
                if(code === 1){
                    state.alerOrder = data
                }
            })

        },
        //修改订单信息 即退款 完成订单等
        changeOrder({dispatch, commit, state, getters},{oFlag, data, reload}){
            // return
            axios({
                method: 'post',
                url:'http://localhost:3003/changeOrder',
                data:{
                    oFlag,
                    data
                }
            }).then((res) =>{
                
                if(res.data.code === 1){
                    //关闭即可
                    Indicator.close()
                    if(reload) return reload()             
                }
            })
        },

//----------------------------------------商店的
        //专门拿取数据
        getShopInfo({commit, state, getters}, params){
            return new Promise((resolve, reject) =>{
                axios({
                    method: 'get',
                    url:'http://localhost:3003/getShop',
                    params
                }).then((res) =>{
                    if(res.data.code === 1) return resolve(res.data.data)
                    reject()
                    // commit('setState', res.data.data)
                })
            })
        },
        //专门修改数据的  
        changeShopLogin({dispatch, commit, state, getters}, {shopId, data}){

            return new Promise((resolve, reject) =>{
                axios({
                    method: 'post',
                    url:'http://localhost:3003/changeShopLogin',
                    data: {
                        shopId,
                        data
                    }
                }).then((res) =>{
                    const {code} = res.data

                    if(code === 1) return resolve()

                    reject()
                })
            })
        },

        //专门修改数据的  
        changeShopFood({dispatch, commit, state, getters}, {shopId, data}){
            return new Promise((resove, reject) =>{
                axios({
                    method: 'post',
                    url:'http://localhost:3003/changeFood',
                    data: {
                        shopId,
                        data
                    }
                }).then((res) =>{
    
                    const {code, data} = res.data
                    if(code === 1) return resove()
                    reject()
                })
            })
        },

        //删除
        removeShopInfo({dispatch, commit, state, getters}, {shopId, data}){
            return new Promise((resove, reject) =>{
                axios({
                    method: 'post',
                    url:'http://localhost:3003/removeShopInfo',
                    data: {
                        shopId,
                        data
                    }
                }).then((res) =>{
    
                    const {code, data} = res.data
                    if(code === 1) return resove()
                    reject()
                })
            })
        },

        //修改商店的月销售 以及评论问题
        changeGoodInfo({dispatch, commit, state, getters}, {sname, data, foodMenu, commitFlag}){
            axios({
                method: 'post',
                url:'http://localhost:3003/changeGood',
                data: {
                    foodMenu,
                    sname,
                    commitFlag,
                    data
                }
            }).then((res) =>{
                
                if(res.data.code === 1){
                    Indicator.close()
                    if(commitFlag) return commit('setToast', '感谢您的评论')
                    commit('setToast', '付款成功')
                }
            })
        },


        //商家宣布破产
        removeShop({commit, state, getters}, params){
            console.log(params)
            axios({
                method: 'get',
                url:'http://localhost:3003/removeShop',
                params,
            }).then((res) =>{
               
               if(res.data.code === 1) {
                console.log(res.data.data)
                return commit('setState', {
                    // 清空商家的所有商店信息
                    type: '普通用户',
                    shopPhone: '',
                    shopAddress: '',
                    shopBgc: '',
                    sname: '',
                    shopId: '',
                    statusName: '',
                    goodMenu: [],
                    couponMenu:[]
                  }) 
               }

               commit('setToast', '破产失败，请稍后操作')
            })
        },
//----------------------------------------首页的获取商品 以及搜索
        getHome({commit, state, getters}, reload){
            axios({
                method: 'get',
                url:'http://localhost:3003/homeShop'
            }).then((res) =>{
               const {data, code} = res.data
               if(code === 1){
                  state.homeShopList = data
                  if(reload){
                      reload()
                  }
               }                
            })
        },

        //搜素 商品的
        getSeach({commit, state, getters}, {sname, reload}){
            axios({
                method: 'get',
                url:'http://localhost:3003/homeSeach',
                params: {sname}
            }).then((res) =>{
               
               const {data, code} = res.data
               if(code === 1){
                    state.homeShopList = data
                    Indicator.close()
                    reload()
                    return
               }

               Indicator.close()
               commit('setToast', '暂无该搜索内容的相关信息')
            })
        },

    }
})

export default store

